﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Text;
using System.Data.SqlClient;

namespace EDMS.Admin.Notice
{
    public partial class NoticeInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                initSelection();
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            LinkButton li = (LinkButton)sender;
            int id = Convert.ToInt32(li.CommandArgument);
            using (DataClassesDataContext dc = new DataClassesDataContext())
            {
                var result = dc.Xjgg.FirstOrDefault(x => x.ggbh == id);
                if (result != null)
                {
                    dc.Xjgg.DeleteOnSubmit(result);
                    dc.SubmitChanges();
                    Response.Write("<script>alert('删除成功！');</script>");
                }
            }
        }
        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (txtGgbt.Text.Equals(""))
            {
                Frame.Global.GlobalFunctions.Alert("请输入公告标题！");
            }
            else if (txtGgnr.Text.Equals(""))
            {
                Frame.Global.GlobalFunctions.Alert("请输入公告内容！");
            }
            else if (txtFbr.Text.Equals(""))
            {
                Frame.Global.GlobalFunctions.Alert("请填写发布人！");
            }
            else
            {
                insertNotice();
            }

        }

        protected void btnEsc_Click(object sender, EventArgs e)
        {
            txtGgbt.Text = "";
            txtFbr.Text = "";
            txtGgnr.Text = "";
            txtFbsj.Text = "";
        }
        //插入公告
        void insertNotice()
        {
            using (DataClassesDataContext dc = new DataClassesDataContext())
            {
                Xjgg gg = new Xjgg();
                gg.ggbt = txtGgbt.Text;
                gg.nr = txtGgnr.Text;
                gg.fbr = txtFbr.Text;
                gg.fbsj = Convert.ToDateTime(txtFbsj.Text);
                gg.flag = "A";
                gg.xjbh = txtXjbh.Text;
                dc.Xjgg.InsertOnSubmit(gg);
                dc.SubmitChanges();
                Frame.Global.GlobalFunctions.Alert("选举公告添加成功！");
            }
        }

        //条件检索
        protected void CBPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            string SqlFormat = @"select ggbh,ggbt,nr,fbr,fbsj from xjgg where {0} 1=1";
            StringBuilder strb = new StringBuilder();
            List<SqlParameter> Params = new List<SqlParameter>();
            if (!string.IsNullOrEmpty(txtXjbhs.Text.Trim()))
            {
                strb.Append("xjbh like ''+@bt+'%'  AND ");
                Params.Add(new SqlParameter("@bt", txtXjbhs.Text.ToString()));
            }
            //if (!string.IsNullOrEmpty(txtFbrc.Text.Trim()))
            //{
            //    strb.Append("fbr like ''+@fbr+'%'  AND ");
            //    Params.Add(new SqlParameter("@fbr", txtFbrc.Text.ToString()));
            //}
            //if (!string.IsNullOrEmpty(deFbsjc.Text.Trim()))
            //{
            //    strb.Append("fbsj like ''+@fbsj+'%'  AND ");
            //    Params.Add(new SqlParameter("@fbsj", deFbsjc.Value.ToString()));
            //}
            this.gvNotice.DataSourceID = "";
            gvNotice.DataSource = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(string.Format(SqlFormat, strb.ToString()), Params.ToArray());
            gvNotice.DataBind();
        }
        //清空条件
        //protected void btnQk_Click(object sender, EventArgs e)
        //{
        //    txtFbrc.Text = "";
        //    txtGgbtc.Text = "";
        //    deFbsjc.Text = "";
        //}

        protected void btnXg_Click(object sender, EventArgs e)
        {
            if (txtGgbtx.Text.Equals(""))
            {
                Frame.Global.GlobalFunctions.Alert("请输入公告标题！");
            }
            else if (txtGgnrx.Text.Equals(""))
            {
                Frame.Global.GlobalFunctions.Alert("请输入公告内容！");
            }
            else if (txtFbrx.Text.Equals(""))
            {
                Frame.Global.GlobalFunctions.Alert("请填写发布人！");
            }
            else
            {
                updateNotice();
            }
        }
        //更新
        void updateNotice()
        {
            using (DataClassesDataContext dc = new DataClassesDataContext())
            {
                var gg = dc.Xjgg.Select(s => s)
                   .Where(s => s.ggbh == Convert.ToInt32(txtBh.Text.ToString()))
                   .SingleOrDefault();
                gg.ggbt = txtGgbtx.Text;
                gg.nr = txtGgnrx.Text;
                gg.fbr = txtFbrx.Text;
                gg.fbsj = Convert.ToDateTime(txtFbsjx.Text);
                gg.xjbh = txtXjbhx.Text;
                dc.SubmitChanges();
                Frame.Global.GlobalFunctions.Alert("选举公告修改成功！");
            }
        }
        //初始化数据
        private void initSelection()
        {
            using (DataClassesDataContext dc = new DataClassesDataContext())
            {
                var sel = from s in dc.Xjdj
                          where s.xjbh == Request.QueryString["SID"]
                          select new
                          {
                              bh = s.xjbh,
                              mc = s.xjmc,
                              sj = s.xjsj,
                              jssj = s.wtjssj,
                              bj = s.flag,
                          };
                foreach (var i in sel)
                {
                    txtXjbhs.Text = i.bh;
                    txtXjmcs.Text = i.mc;
                    txtXjsjs.Text = i.sj.ToString();
                }
            }
        }

        protected void pcAddNotice_Load(object sender, EventArgs e)
        {
            txtXjbh.Text = txtXjbhs.Text;
            txtFbsj.Text = DateTime.Now.ToShortDateString();
        }

        protected void pcUpdateNotice_Load(object sender, EventArgs e)
        {
            txtXjbhx.Text = txtXjbhs.Text;
            txtFbsjx.Text = DateTime.Now.ToShortDateString();
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {

        }

        void bind()
        {
            using (DataClassesDataContext dc = new DataClassesDataContext())
            {
               var no = dc.Xjgg.FirstOrDefault(x => x.xjbh == Request.QueryString["SID"]);
            }
            this.gvNotice.DataSource = "no";
            this.gvNotice.DataBind();
        }
    }
}